{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7d6be7ef-ba90-4e90-a638-0dc3a3daa6c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1个文档的tf iDF的信息\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'revese' is an invalid keyword argument for sort()",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 39\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m第\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m个文档的tf iDF的信息\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(index\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m))\n\u001b[0;32m     38\u001b[0m scores \u001b[38;5;241m=\u001b[39m {word:tf_idf(word,count,count_list) \u001b[38;5;28;01mfor\u001b[39;00m word \u001b[38;5;129;01min\u001b[39;00m count}\n\u001b[1;32m---> 39\u001b[0m scored_word\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mscores\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m:\u001b[49m\u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrevese\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[0;32m     41\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m word,score \u001b[38;5;129;01min\u001b[39;00m scored_word:\n\u001b[0;32m     42\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mword:\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m,, TF, IDF:\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(word,\u001b[38;5;28mround\u001b[39m(score,\u001b[38;5;241m5\u001b[39m))) \n",
      "\u001b[1;31mTypeError\u001b[0m: 'revese' is an invalid keyword argument for sort()"
     ]
    }
   ],
   "source": [
    "corpus = [\n",
    "    \"这 是 第一个 文档\",\n",
    "    \"这是 第二个 文档\",\n",
    "    \"这是 最后 一个 文档\",\n",
    "    \"现在 没有 文档 了 文档\"\n",
    "]\n",
    "\n",
    "# 手动实现\n",
    "\n",
    "words_list=[]\n",
    "\n",
    "for corpu in corpus:\n",
    "    words_list.append(corpu.split())\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "count_list=[]\n",
    "for words in words_list:\n",
    "    count=Counter(words)\n",
    "    count_list.append(count)\n",
    "\n",
    "\n",
    "\n",
    "import math\n",
    "\n",
    "def tf(word,count):\n",
    "    return count[word]/sum(count.values())\n",
    "    \n",
    "def idf(word,count_list):\n",
    "    n_contain = sum([1 for count in count_list if word in count])\n",
    "    return math.log(len(count_list)/(1+n_contain))\n",
    "\n",
    "def tf_idf(word,count,count_list):\n",
    "    return tf(word,count) * idf(word,count_list)\n",
    "\n",
    "for index,count in enumerate(count_list):\n",
    "    print(\"第{}个文档的tf iDF的信息\".format(index+1))\n",
    "    scores = {word:tf_idf(word,count,count_list) for word in count}\n",
    "    scored_word=sorted(scores.items(), key=lambda x:x[1], reverse=True)\n",
    "\n",
    "    for word,score in scored_word:\n",
    "        print(\"word:{},, TF, IDF:{}\".format(word,round(score,5))) \n",
    "    \n",
    "# Gemsim\n",
    "\n",
    "\n",
    "\n",
    "# Sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe6bcf1a-c488-43f2-bf51-9912933232d7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
